package com.edward.service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ActHarvest;
import com.edward.model.Harvest;
import com.edward.model.OtherHarvest;
import com.edward.model.SelfHarvest;


public class SelfHarvestManager extends HibernateDaoSupport {
	
	public void addSelfHarvest(SelfHarvest selfHarvest) {
		
		getHibernateTemplate().save(selfHarvest);
	}
	
	public void updateSelfHarvest(SelfHarvest selfHarvest) {
		
		getHibernateTemplate().update(selfHarvest);
	}
	

	public void deleteSelfHarvest(int id) {
		
		getHibernateTemplate().delete(getSelfHarvest(id));
	}
	
	
	public SelfHarvest getSelfHarvest(int id){
		SelfHarvest d =(SelfHarvest)getHibernateTemplate().get(SelfHarvest.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<SelfHarvest> listSelfHarvest() {
		return getHibernateTemplate().find("from SelfHarvest");
	}

	@SuppressWarnings("unchecked")	
	public void addSelf (SelfHarvest selfHarvest){
		selfHarvest.setTag("t");
		getHibernateTemplate().save(selfHarvest);
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(selfHarvest.getDate());
		String m = mm.format(selfHarvest.getDate());
		List <SelfHarvest> SelfList = getHibernateTemplate().find("from SelfHarvest s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "' and s.tag is null");
		if(SelfList.size()>0){
			for (SelfHarvest sh : SelfList) {
				if(sh.getType() == null || sh.getType().equals(selfHarvest.getType())){
					SelfHarvest s  = selfHarvest;
					sh.setCost(sh.getCost() + s.getCost());
					sh.setBalance(sh.getBalance() + s.getBalance());
					sh.setOther(sh.getOther() + s.getOther());
					sh.setLoss(sh.getLoss() + s.getLoss());
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					sh.setGrossProfit(sh.getIncome()- sh.getCost());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);
					}
					getHibernateTemplate().update(selfHarvest);
				}
			}
		}else{
			SelfHarvest sh = new SelfHarvest();
			List <SelfHarvest> l = new ArrayList<SelfHarvest>();
			sh.setDate(selfHarvest.getDate());
			sh.setCost(selfHarvest.getCost());
			sh.setBalance(selfHarvest.getBalance());
			sh.setOther(selfHarvest.getOther());
			sh.setLoss(selfHarvest.getLoss());
			sh.setSelfs(l);
			addSelfHarvest(sh);
			for(int i=0; i<5;i++){
					SelfHarvest st = new SelfHarvest();
					if(selfHarvest.getType().equals(judge(i))){
						st.setCost(sh.getCost());
						st.setBalance(sh.getBalance());
						st.setOther(sh.getOther());
						st.setLoss(sh.getLoss());
					}
					st.setType(judge(i));
					st.setDate(selfHarvest.getDate());
					addSelfHarvest(st);
					sh.getSelfs().add(st);
			}
			updateSelfHarvest(sh);
		}
		
	}
	
	public void updateSelf (SelfHarvest s){
		updateSelfHarvest(s);
	}
	
	@SuppressWarnings("unchecked")	
	public void deleteSelf(int id){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		SelfHarvest ss = getSelfHarvest(id);
		String y = yy.format(ss.getDate());
		String m = mm.format(ss.getDate());
		List <SelfHarvest>l = getHibernateTemplate().find("from SelfHarvest s where s.type is null and year(s.date) = '" + y + "' and month(s.date) = '" + m + "'");
		SelfHarvest st = l.get(0);
		SelfHarvest self = st.getSelfs().get(judge(ss.getType())) ;
		st.setCost(st.getCost() - ss.getCost() );
		st.setBalance(st.getBalance() - ss.getBalance());
		st.setOther(st.getOther() - ss.getOther());
		st.setLoss(st.getLoss() - ss.getLoss());
		st.setNetProfit(st.getIncome() - st.getCost() - st.getFee() - st.getTax() + st.getBalance() - st.getOther() - st.getLoss());
		st.setGrossProfit(st.getIncome()- st.getCost());
		if(st.getNetProfit()==0||st.getIncome()==0){
			st.setRate(0);
		}else{
			st.setRate(st.getNetProfit() / st.getIncome()*100);
			int i = 0;
			double dou ;
			i = (int) (st.getRate() * 1000);
			dou = i;
			st.setRate(dou/1000);
		}
		self.setBalance(self.getBalance() - ss.getBalance());
		self.setCost(self.getCost() - ss.getCost());
		self.setLoss(self.getLoss() - ss.getLoss());
		self.setNetProfit(self.getNetProfit() - ss.getNetProfit());
		self.setNetProfit(self.getIncome() - self.getCost() - self.getFee() - self.getTax() + self.getBalance() - self.getOther() - self.getLoss());
		self.setGrossProfit(self.getIncome()- self.getCost());
		if(self.getNetProfit()==0||self.getIncome()==0){
			self.setRate(0);
		}else{
			self.setRate(self.getNetProfit() / self.getIncome()*100);
			int i = 0;
			double dou ;
			i = (int) (self.getRate() * 1000);
			dou = i;
			self.setRate(dou/1000);
		}
		updateSelfHarvest(self);
		updateSelfHarvest(st);
		deleteSelfHarvest(id);
	}
	
	
	@SuppressWarnings("unchecked")	
	public void updateSelfSum (SelfHarvest s){
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(s.getDate());
		String m = mm.format(s.getDate());
		List <SelfHarvest>l = getHibernateTemplate().find("from SelfHarvest s where s.type is null and year(s.date) = '" + y + "' and month(s.date) = '" + m + "'");
		SelfHarvest st = l.get(0);
		SelfHarvest ss = getSelfHarvest(s.getId());
		SelfHarvest self = st.getSelfs().get(judge(s.getType())) ;
		st.setCost(st.getCost() + s.getCost() - ss.getCost() );
		st.setBalance(st.getBalance() + s.getBalance() - ss.getBalance());
		st.setOther(st.getOther() + s.getOther() - ss.getOther());
		st.setLoss(st.getLoss() + s.getLoss() - ss.getLoss());
		st.setNetProfit(st.getIncome() - st.getCost() - st.getFee() - st.getTax() + st.getBalance() - st.getOther() - st.getLoss());
		st.setGrossProfit(st.getIncome()- st.getCost());
		if(st.getNetProfit()==0||st.getIncome()==0){
			st.setRate(0);
		}else{
			st.setRate(st.getNetProfit() / st.getIncome()*100);
			int i = 0;
			double dou ;
			i = (int) (st.getRate() * 1000);
			dou = i;
			st.setRate(dou/1000);
		}
		self.setBalance(self.getBalance() + s.getBalance() - ss.getBalance());
		self.setCost(self.getCost() + s.getCost() - ss.getCost());
		self.setLoss(self.getLoss() + s.getLoss() - ss.getLoss());
		self.setNetProfit(self.getIncome() - self.getCost() - self.getFee() - self.getTax() + self.getBalance() - self.getOther() - self.getLoss());
		self.setGrossProfit(self.getIncome()- self.getCost());
		self.setOther(self.getOther() + s.getOther() - ss.getOther());
		if(self.getNetProfit()==0||self.getIncome()==0){
			self.setRate(0);
		}else{
			self.setRate(self.getNetProfit() / self.getIncome()*100);
			int i = 0;
			double dou ;
			i = (int) (self.getRate() * 1000);
			dou = i;
			self.setRate(dou/1000);
		}
		updateSelfHarvest(self);
		updateSelfHarvest(st);
	}
		
	
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		String y ,m;
		if(query.equals("")){
			SimpleDateFormat yy = new SimpleDateFormat("yyyy");
			SimpleDateFormat mm = new SimpleDateFormat("MM");
			y = yy.format(new Date());
			m = mm.format(new Date());
		}else{
			y = query.split("-")[0];
			m = query.split("-")[1];
			
		}
		
		List <SelfHarvest>l = getHibernateTemplate().find("from SelfHarvest s where s.type is null and year(s.date) = '" + y + "' and month(s.date) = '" + m + "' order by s.date");
		List <SelfHarvest> list = new ArrayList<SelfHarvest>();
		SelfHarvest st = new SelfHarvest();
		for(SelfHarvest self : l ){
			for(SelfHarvest s :self.getSelfs()){
				list.add(s);
			}
			st.setIncome(st.getIncome() + self.getIncome());
			st.setCost(st.getCost() + self.getCost());
			st.setFee(st.getFee() + self.getFee());
			st.setTax(st.getTax() + self.getTax());
			st.setBalance(st.getBalance() + self.getBalance());
			st.setOther(st.getOther() + self.getOther() );
			st.setLoss(st.getLoss() + self.getLoss());
			st.setGrossProfit(st.getGrossProfit() + self.getGrossProfit());
			st.setNetProfit(st.getNetProfit() + self.getNetProfit());
			st.setDate(null);
			self.setSelfs(null);
			list.add(self);
		}
		if(st.getNetProfit()==0||st.getIncome()==0){
			st.setRate(0);
		}else{
			st.setRate(st.getNetProfit() / st.getIncome()*100);
			int i = 0;
			double dou ;
			i = (int) (st.getRate() * 1000);
			dou = i;
			st.setRate(dou/1000);
		}
		list.add(st);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <SelfHarvest>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
	
	
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerSelf(int start,int limit,SelfHarvest selfHarvest) {
		String y ,m;
			SimpleDateFormat yy = new SimpleDateFormat("yyyy");
			SimpleDateFormat mm = new SimpleDateFormat("MM");
			y = yy.format(selfHarvest.getDate());
			m = mm.format(selfHarvest.getDate());
		
		String sql = "from SelfHarvest s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "' and s.tag = 't'";	
		if(selfHarvest.getType() !=null){
			sql += " and s.type = '" + selfHarvest.getType() + "'";  
		}
			
		List <SelfHarvest>list = getHibernateTemplate().find(sql);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <SelfHarvest>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
	
	
	

	@SuppressWarnings("unchecked")	
	public PagerModel pagerSum(int start,int limit,String query) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		List <SelfHarvest>list1 = getHibernateTemplate().find("from SelfHarvest s where s.type is null and year(s.date) = '" + query + "' order by s.date");
		List <ActHarvest>list2 = getHibernateTemplate().find("from ActHarvest a where a.type is null and year(a.date) = '" + query + "' order by a.date");
		List <OtherHarvest>list3 = getHibernateTemplate().find("from OtherHarvest o where o.type is null and year(o.date) = '" + query + "' order by o.date");
		List <Harvest> list = new ArrayList<Harvest>();
		
		Harvest ht = new Harvest();
		Date date = null;
		for(int i=0 ;i<12;i++){
			Harvest h = new Harvest();
			 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");   
			 String str = "-";
			 if(i<9){
				 str = str + "0" +(i+1);
			 }else{
				 str = str + (i+1);
			 }
			    	try {
						date = df.parse(query+str);
					} catch (ParseException e) {
						e.printStackTrace();
					}
			    	h.setDate(date);
			    	list.add(h);
		}
		
		for(int i=0 ;i<12;i++){
			   
			    
		if(list1.size() > i){
			SelfHarvest self = list1.get(i);
			int e = estimate(self.getDate());
			Harvest h = list.get(e);
			h.setIncome(self.getIncome());
			h.setCost(self.getCost());
			h.setFee(self.getFee());
			h.setTax(self.getTax());
			h.setBalance(self.getBalance());
			h.setOther(self.getOther() );
			h.setLoss(self.getLoss());
			h.setGrossProfit(self.getGrossProfit());
			h.setSelfnet(self.getNetProfit());
			h.setRate(self.getRate());
			h.setDate(self.getDate());
			ht.setIncome(ht.getIncome() + self.getIncome());
			ht.setCost(ht.getCost() + self.getCost());
			ht.setFee(ht.getFee() + self.getFee());
			ht.setTax(ht.getTax() + self.getTax());
			ht.setBalance(ht.getBalance() + self.getBalance());
			ht.setOther(ht.getOther() + self.getOther() );
			ht.setLoss(ht.getLoss() + self.getLoss());
			ht.setGrossProfit(ht.getGrossProfit() + self.getGrossProfit());
			ht.setSelfnet(ht.getSelfnet() + self.getNetProfit());
		}
		if(list2.size() > i){
			
			ActHarvest act = list2.get(i);
			int e = estimate(list2.get(i).getDate());
			Harvest h = list.get(e);
			h.setSale(act.getSale() );
			h.setActRate(act.getActRate());
			h.setDiffPrice(act.getDiffPrice());
			h.setActnet(act.getNetProfit());
			ht.setSale(ht.getSale() + act.getSale() );
			ht.setActRate(ht.getActRate() + act.getActRate());
			ht.setDiffPrice(ht.getDiffPrice() + act.getDiffPrice());
			ht.setActnet(ht.getActnet() + act.getNetProfit());
		}
		if(list3.size() > i){
			OtherHarvest other = list3.get(i);
			int e = estimate(list3.get(i).getDate());
			Harvest h = list.get(e);
			h.setOcost(other.getCost() );
			h.setOincome(other.getIncome());
			ht.setOcost(ht.getOcost() + other.getCost() );
			ht.setOincome(ht.getOincome() + other.getIncome());
		}
		}
		ht.setNet(ht.getActnet() + ht.getSelfnet() + ht.getOincome() - ht.getOcost());
		if(ht.getSelfnet()==0||ht.getIncome()==0){
			ht.setRate(0);
		}else{
			ht.setRate(ht.getSelfnet()/ht.getIncome()*100);
			int i = 0;
			double d ;
			i = (int) (ht.getRate() * 1000);
			d = i;
			ht.setRate(d/1000);
		}
		
		
		if(ht.getNet() != 0){
			ht.setNetRate(ht.getNet() / (ht.getIncome() + ht.getSale() + ht.getOincome()) * 100);
			int j = 0;
			double d ;
			j = (int) (ht.getNetRate() * 1000);
			d = j;
			ht.setNetRate(d/1000);
		}else{
			ht.setRate(0);
		}
		for(int i=0;i<12;i++){
			Harvest h = list.get(i);
			h.setNet(h.getActnet() + h.getSelfnet() + h.getOincome() - h.getOcost());
			if(h.getNet() != 0){
				h.setNetRate(h.getNet() / (h.getIncome() + h.getSale() + h.getOincome()) * 100);
				int j = 0;
				double d ;
				j = (int) (h.getNetRate() * 1000);
				d = j;
				h.setNetRate(d/1000);
			}else{
				h.setRate(0);
			}
		}
		
		
		list.add(ht);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Harvest>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
	
	public int judge (String s ){
		if(s.equals("汽车")){
			return 0;
		}else if (s.equals("摩托车")){
			return 1;
		}else if (s.equals("汽车配件")){
			return 2;
		}else if (s.equals("摩托车配件")){
			return 3;
		}else{
			return 4;
		}
	}
	
	public String judge (int i){
		if(i == 0){
			return "汽车";
		}else if (i == 1){
			return "摩托车";
		}else if (i == 2){
			return "汽车配件";
		}else if (i == 3){
			return "摩托车配件";
		}else{
			return "其他";
		}
	}
	
	
	public int estimate (Date date){
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM"); 
		 String s  = df.format(date);
		if(s.subSequence(5, 7).equals("01")) return 0;
		else if(s.subSequence(5, 7).equals("02")) return 1;
		else if(s.subSequence(5, 7).equals("03")) return 2;
		else if(s.subSequence(5, 7).equals("04")) return 3;
		else if(s.subSequence(5, 7).equals("05")) return 4;
		else if(s.subSequence(5, 7).equals("06")) return 5;
		else if(s.subSequence(5, 7).equals("07")) return 6;
		else if(s.subSequence(5, 7).equals("08")) return 7;
		else if(s.subSequence(5, 7).equals("09")) return 8;
		else if(s.subSequence(5, 7).equals("10")) return 9;
		else if(s.subSequence(5, 7).equals("11")) return 10;
		else return 11;
		
	}
	


}



